import axios from 'axios'

import {
    ElLoading, ElMessage
} from 'element-plus'


/*eslint-disable */
//设置超时时间
axios.defaults.timeout = 60000


//对外接口
export function request({
    method,
    url,
    params,
    isLoading
}) {
    if (method === 'Get') {
        return get(url, params)
    } else if (method === 'Post') {
        return post(url, params, isLoading)
    } else if (method === 'Del') {
        return del(url, params, isLoading)
    } else if (method === 'Put') {
        return put(url, params, isLoading)
    }
}


// 封装get方法
function get(url, params, isLoading = true) {
    if (isLoading) {
        load = ElLoading.service({
            // body: true,
            lock: true,
            // spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.2)',
            customClass: 'osloading' // 重点，修改样式类名
        })
    }
    return new Promise((resolve, reject) => {
        axios
            .get(url, {
                params
            })
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

let load = null

//封装delete方法
function del(url, params, isLoading = true) {
    if (isLoading) {
        load = ElLoading.service({
            // body: true,
            lock: true,
            // spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.2)',
            customClass: 'osloading' // 重点，修改样式类名
        })
    }
    return new Promise((resolve, reject) => {
        axios
            .delete(url, params)
            .then(res => {
                resolve(res)

            })
            .catch(err => {
                reject(err)
            })

    })
}

// 封装post方法
function post(url, params, isLoading = true) {
    if (isLoading) {
        load = ElLoading.service({
            // body: true,
            lock: true,
            // spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.2)',
            customClass: 'osloading' // 重点，修改样式类名
        })
    }
    return new Promise((resolve, reject) => {
        axios
            .post(url, params)
            .then(res => {
                resolve(res)

            })
            .catch(err => {
                console.log(err)
                reject(err)
            })
    })
}

//封装put方法
function put(url, params, isLoading = true) {
    if (isLoading) {
        load = ElLoading.service({
            // body: true,
            lock: true,
            // spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.2)',
            customClass: 'osloading' // 重点，修改样式类名
        })
    }
    return new Promise((resolve, reject) => {
        axios
            .put(url, params)
            .then(res => {
                resolve(res)

            })
            .catch(err => {
                reject(err)

            })
    })
}

//请求拦截(请求发出前处理请求)
axios.interceptors.request.use(
    confirm => {
        return confirm
    },
    error => {
        return Promise.error(error)
    }
)

// 响应拦截器（处理响应数据）
axios.interceptors.response.use(
    response => {
        setTimeout(() => {
            if (load) {
                load.close()
            }
        }, 200)
        if (response.status === 200) {
            if (response.data) {
                if (response.data.code == undefined || response.data.code === 200) {
                    return Promise.resolve(response)
                } else {
                    ElMessage.error({
                        message: response.data.msg,
                        type: 'error'
                    });
                    return Promise.reject(response.data)
                }

            } else {
                ElMessage.error({
                    message: response.data.msg,
                    type: 'error'
                });
                return Promise.reject(response.data)
            }

        } else {
            ElMessage.error({
                message: response,
                type: 'error'
            });
            //这个地方可以由后台编辑状态码区分不同情况，做不同的逻辑处理
            return Promise.reject(response)
        }
    },
    error => {
        setTimeout(() => {
            if (load) {
                load.close()
            }
        }, 200)
        return Promise.reject(error.response)
    }
)
